package com.ltmonitor.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.Department;
import com.ltmonitor.vo.PaginateResult;
import com.ltmonitor.web.util.OnlineUser;

import net.sf.json.JSONObject;

/**
 * 夜间行驶查询页面
 * @author shinion
 *
 */
@Controller
@RequestMapping("/nightDrive")
public class NightDrive extends PaginateAction {

	
	/**显示页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/view.action")
	public String view(Model model) {
		model.addAttribute("message","success");
		
		return"alarm/nightDrive";
	}
	
	
	/**
	 * 获取查询数据，以分页形式返回
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/getNightDrivePageData.action")
	public @ResponseBody PaginateResult getNightDrivePageData(@RequestBody String jsonStr) {
		PaginateResult result = null;
		try {
			result = new PaginateResult();
			//用于获取用户权限信息
			OnlineUser user = super.getOnlineUser();
			if(user != null)
			{
				Map<String, Object> map = JSONObject.fromObject(jsonStr);
				//分页查询转换
				Integer pageNum = (Integer) map.get("pageNumber");
				Integer pageSize = (Integer) map.get("pageSize");
				if(pageNum == null)
					pageNum=1;
				if(pageSize == null)
					pageSize = 15;
				
				//获取用户id
				long userId = user.getEntityId();
				map.put("userId", userId);
				//根据用户id获取部门列表
				Integer depId = (Integer)map.get("depId");
				if(depId == null)
					depId = 0;
				
				if (depId >0) {
					List<Long> depIdList = super.getDepartmentService()
							.getDepIdList(depId);
					map.put("depIdList", depIdList);
					map.remove("depId");
				}else {
					//如果部门编号为空，查询所有，那么通过userId获取部门编号
					if(userId > 0) {
						List<Department> list = super.getDepartmentService().getDepartmentListByUserId(userId);
						if(list.size() > 0) {
							List<Long> depIdList2 = new ArrayList<Long>();
							for(Department temp:list) {
								depIdList2.add(temp.getEntityId());
							}
							map.put("depIdList", depIdList2);
							map.remove("depId");
						}
					}
				}
				System.out.println("++++++++++++++++++++++++");
				System.out.println("++++++++++++++++++++++++");
				System.out.println(map);
				System.out.println("++++++++++++++++++++++++");
				System.out.println("++++++++++++++++++++++++");
				
				//查询语句
				String queryId = "nightDrive.selectNightDrivePageData";
				//查询
				result = queryService.queryByPagination(queryId, map, pageNum, pageSize);
			}//end if user	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
		return result;
	}
	
	
	
}
